import {_decorator, Animation, AnimationClip, Component, SpriteFrame} from 'cc';
import {FSM_TYPE_ENUM, PARAMS_NAME_ENUM} from "../Enums";
import State from "./State";
import {SubStateMachine} from "./SubStateMachine";

const { ccclass, property } = _decorator;

const whiteList = ['turn']; // 播放完毕恢复idle动画
type ParamValueType = boolean | number;

interface IParamValue {
    type: FSM_TYPE_ENUM,
    value: ParamValueType
}

export const getInitParamTrigger = () => {
    return {type: FSM_TYPE_ENUM.TRIGGER, value: false}
}
export const getInitParamNumber = () => {
    return {type: FSM_TYPE_ENUM.NUMBER, value: 0}
}

@ccclass('StateMachine')
export abstract class StateMachine extends Component {

    params: Map<string, IParamValue> = new Map();
    stateMachines: Map<string, State | SubStateMachine> = new Map();
    animationComponent: Animation;
    private _currentState: State | SubStateMachine = null;
    waitingList: Array<Promise<SpriteFrame[]>> = []

    get currentState(){
        return this._currentState;
    }
    set currentState(newState) {
        this._currentState = newState;
        this._currentState.run();
    }

    getParam(paramName: string) {
        if(this.params.has(paramName))
            return this.params.get(paramName).value;
    }
    setParam(paramName: string, value: ParamValueType){
        if(this.params.has(paramName)){
            this.params.get(paramName).value = value;

            this.run();
            this.resetTrigger();
        }
    }

    resetTrigger() {
        for (const [_, value] of this.params) {
            if(value.type === FSM_TYPE_ENUM.TRIGGER)
                value.value = false;
        }
    }

    abstract init(): void
    abstract run(): void

}

